from __future__ import annotations

from dataclasses import dataclass
from math import tan, radians


@dataclass
class SlopeGeometry:
    slope_ratio: float
    top_length: float
    top_width: float
    note: str


def excavation_top_size(bottom_length: float, bottom_width: float, depth: float, soil_friction_deg: float) -> SlopeGeometry:
    """放坡计算：按 1:m，其中 m=1/tanφ。"""

    m = 1.0 / max(1e-6, tan(radians(soil_friction_deg)))
    top_length = bottom_length + 2.0 * m * depth
    top_width = bottom_width + 2.0 * m * depth
    return SlopeGeometry(slope_ratio=m, top_length=top_length, top_width=top_width, note="按φ估算放坡")


@dataclass
class EarthPressure:
    active_kPa: float
    passive_kPa: float
    at_depth_kPa: float
    note: str


def rankine_pressure(unit_weight_kN_m3: float, depth_m: float, friction_deg: float, wall_friction_deg: float = 0.0) -> EarthPressure:
    """朗肯土压力计算。"""

    import math
    phi = radians(friction_deg)
    Ka = math.tan(math.pi / 4 - phi / 2) ** 2
    Kp = math.tan(math.pi / 4 + phi / 2) ** 2
    p_a = Ka * unit_weight_kN_m3 * depth_m
    p_p = Kp * unit_weight_kN_m3 * depth_m
    return EarthPressure(active_kPa=p_a, passive_kPa=p_p, at_depth_kPa=p_a, note="朗肯理论")


